#include "admin.h"
#include "ui_admin.h"
#include <QMessageBox>
#include <QDebug>
#include<QtDebug>

Admin::Admin(QSqlDatabase &d) :
//    QWidget(parent),
    db(d),
    ui(new Ui::Admin),
    userManePage(nullptr),
    bookManePage(nullptr),
    borManePage(nullptr),
    hisManePage(nullptr)
{
    ui->setupUi(this);
    initPage();
}

Admin::~Admin()
{
    delete ui;
}

void Admin::on_backBut_clicked()
{
    emit destroyed();
    close();
}

void Admin::on_closeBut_clicked()
{
    QMessageBox messageBox(QMessageBox::NoIcon,
                                   "退出", "你确定要退出吗?",
                                   QMessageBox::Yes | QMessageBox::No);
    int result=messageBox.exec();

    switch (result)
    {
    case QMessageBox::Yes:
        qDebug()<<"Yes";
        close();
        break;
    case QMessageBox::No:
        qDebug()<<"NO";
        break;
    default:
        break;
    }
}

void Admin::initPage(){

    userManePage=new UserMana(db);
    bookManePage=new bookMana(db);
    borManePage=new borMane(db);
    hisManePage= new HisMana(db);

    ui->stackedWidget->addWidget(userManePage);
    ui->stackedWidget->addWidget(bookManePage);
    ui->stackedWidget->addWidget(borManePage);
    ui->stackedWidget->addWidget(hisManePage);

    connect(ui->userBut,&QToolButton::clicked,this,&Admin::dealMeun);
    connect(ui->bookBut,&QToolButton::clicked,this,&Admin::dealMeun);
    connect(ui->jieBut,&QToolButton::clicked,this,&Admin::dealMeun);
    connect(ui->hisBut,&QToolButton::clicked,this,&Admin::dealMeun);
}

void Admin::dealMeun(){
    qDebug()<<sender()->objectName();
    auto str = sender()->objectName();
    do{
        if(str=="userBut"){
            ui->stackedWidget->setCurrentIndex(0);
        }
        else if (str=="bookBut") {
            ui->stackedWidget->setCurrentIndex(1);
        }
        else if(str=="jieBut"){
            ui->stackedWidget->setCurrentIndex(2);
        }
        else {
            ui->stackedWidget->setCurrentIndex(3);
        }

    }while (false);
}
